﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Fly.Common.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class BinaryReaderExtensions
    {
        /// <summary>
        /// 从流中读取指定长度的值并转换为字符串
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="count">The count.</param>
        /// <returns>the characters read from the file, as a string</returns>
        public static string ReadString(this BinaryReader reader, int count)
        {
            if (reader == null)
                throw new ArgumentNullException("reader", "reader is null.");

            var charsWithoutNulls =
                reader.ReadChars(count)
                .TakeWhile(character => character != '\0')
                .ToArray();

            return new string(charsWithoutNulls);
        }
    }
}
